import BaseUtil from '@/BaseUtil';
import InputUtil from '@/InputUtil';
import SolarConfig from '@/SolarConfig';
import TipFrameUtil from '@/TipFrameUtil';
import { Frame } from '@/frame';
import { Easing, Tween } from '@/lib/tween';

export default class FrameTipUtil {
    /**
     *
     * @param icon 图标
     * @param text 文字
     * @param starX 开始位置x
     * @param starY 开始位置y
     * @param toX 目标位置x
     * @param toY 目标位置y
     * @param dur 持续时间
     * @param iconSize 图标大小
     * @param fontSize 字体大小
     * @param showPlayer 显示的玩家
     * @returns
     */
    static showIconText(
        icon: string,
        text: string,
        starX: number,
        starY: number,
        toX: number,
        toY: number,
        dur: number = 0.5,
        iconSize = 0.011,
        fontSize: number = iconSize,
        showPlayer?: player
    ): {
        icon: Frame;
        text: Frame;
    } {
        if (showPlayer != null && GetLocalPlayer() != showPlayer) {
            return;
        }
        let frames = TipFrameUtil._sl_tipIconTextFrameObjectPool.borrowObject();
        frames.icon.setSize(iconSize, iconSize);
        frames.icon.setTexture(icon);
        frames.icon.visible = true;
        //
        frames.text.setText(text);
        frames.text.visible = true;
        frames.text.setFont(fontSize);
        let tween = new Tween({ x: starX, y: starY, s: iconSize, a: 255 })
            .to({ x: toX, y: toY, s: iconSize, a: 155 }, dur * 1000)
            .easing(Easing.Linear.None)
            .onUpdate((temp) => {
                DzFrameSetAlpha(frames.icon.current, temp.a);
                DzFrameSetAbsolutePoint(frames.icon.current, FRAMEPOINT_CENTER, temp.x, temp.y);
                DzFrameSetSize(frames.icon.current, temp.s, temp.s);
            })
            .onComplete((o) => {
                frames.icon.setSize(0.04, 0.04);
                frames.icon.visible = false;
                frames.text.visible = false;
                TipFrameUtil._sl_tipIconTextFrameObjectPool.returnObject(frames);
            });
        tween.start();
        return;
    }
    /**
     * 从屏幕指定位置移动并缩小图标到指定位置 （使用示例：吞噬装备后显示一个图标到UI界面按钮里 方便玩家知道在那里点击可以查看此图标）
     * @param icon
     * @param startX
     * @param startY
     * @param toX
     * @param toY
     * @param dur
     * @param showPlayer
     */
    static showIconMoveIntoAnimation(
        icon: string,
        startX: number = 0.4,
        startY: number = 0.3,
        toX: number = 0.08,
        toY: number = 0.055,
        dur: number = 0.5,
        startSize: number = 0.06,
        toSize: number = 0.025,
        showPlayer?: player
    ): {
        icon: Frame;
        text: Frame;
    } {
        if (showPlayer != null && GetLocalPlayer() != showPlayer) {
            return;
        }
        let frames = TipFrameUtil._sl_tipIconTextFrameObjectPool.borrowObject();
        frames.icon.setSize(0.06, 0.06);
        frames.icon.setTexture(icon);
        frames.icon.visible = true;
        //
        frames.text.visible = false;

        let tween = new Tween({ x: startX, y: startY, s: startSize, a: 255 })
            .to({ x: toX, y: toY, s: toSize, a: 155 }, dur * 1000)
            // .delay(200)
            .easing(Easing.Cubic.InOut)
            .onUpdate((temp) => {
                DzFrameSetAlpha(frames.icon.current, temp.a);
                DzFrameSetAbsolutePoint(frames.icon.current, FRAMEPOINT_CENTER, temp.x, temp.y);
                DzFrameSetSize(frames.icon.current, temp.s, temp.s);
            })
            .onComplete((o) => {
                frames.icon.setSize(0.04, 0.04);
                frames.icon.visible = false;
                frames.text.visible = false;
                TipFrameUtil._sl_tipIconTextFrameObjectPool.returnObject(frames);
            });
        tween.start();
        return frames;
    }

    /**
     * 在UI层显示提示信息 如一些点击图标后条件不足的警告提示 优点是就在鼠标此时的位置显示，符合玩家此时的焦点视区
     * @param tipText  示例 “|cffff0000金币不足”
     * @param framePoint
     * @param dur
     * @param fontSize
     * @param showPlayer
     */
    static showTipTextAtMouse(tipText: string, dur: number = 1, fontSize: number = 0.012, showPlayer?: player): Frame {
        if (showPlayer != null && GetLocalPlayer() != showPlayer) {
            return;
        }
        let frame = TipFrameUtil._sl_tipTextFrameObjectPool.borrowObject();
        frame.setText(tipText);
        frame.visible = true;
        frame.setFont(fontSize);
        DzFrameShow(frame.backdropFrame.handle, true);
        let startX = InputUtil.getMouseSceneX();
        let startY = InputUtil.getMouseSceneY();

        BaseUtil.runLater(
            0.03,
            (count, maxCount) => {
                let y = 0.01 + 0.001 * count;
                frame.setAbsPoint(FRAMEPOINT_BOTTOMLEFT, startX, startY + y);
            },
            30,
            true
        );

        BaseUtil.runLater(dur, () => {
            frame.visible = false;
            DzFrameShow(frame.backdropFrame.handle, false);
            TipFrameUtil._sl_tipTextFrameObjectPool.returnObject(frame);
        });
        return frame;
    }

    static showWarnText(tipText: string, dur: number = 1, fontSize: number = 0.015, showPlayer?: player) {
        return FrameTipUtil.showTipTextAtMouse(SolarConfig.defaultWarnTextColor + tipText, dur, fontSize, showPlayer);
    }

    static showFailText(tipText: string, dur: number = 1, fontSize: number = 0.015, showPlayer?: player) {
        PlaySound('Sound\\Interface\\Error.wav');
        return FrameTipUtil.showTipTextAtMouse(SolarConfig.defaultFailTextColor + tipText, dur, fontSize, showPlayer);
    }

    static showSuccessText(tipText: string, dur: number = 1, fontSize: number = 0.015, showPlayer?: player) {
        return FrameTipUtil.showTipTextAtMouse(SolarConfig.defaultSuccessTextColor + tipText, dur, fontSize, showPlayer);
    }
}
